import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class Player extends EObject{
    private boolean isSeleted =false;
    private boolean isMove =false;
    private boolean isAttack =false;
    private String imagePath;
    private boolean isShowHP =false;
    static int map[][] =Maps.getMap();
    static int N = 1;//北
    static int S = -1;//南
    static int W= -2;//西
    static int E =2;//东
    private int HP =100;

    public boolean isShowHP() {
        return isShowHP;
    }

    public void setShowHP(boolean showHP) {
        isShowHP = showHP;
    }

    public int getHP() {
        return HP;
    }

    public void setHP(int HP) {
        this.HP = HP;
    }

    public boolean isAttack() {
        return isAttack;
    }

    public void setAttack(boolean attack) {
        isAttack = attack;
    }

    private Color color;

    public boolean isMove() {
        return isMove;
    }

    public void setMove(boolean move) {
        isMove = move;
    }

    public boolean isSeleted() {
        return isSeleted;
    }

    public void setSeleted(boolean seleted) {
        isSeleted = seleted;
    }

    public Player(int x, int y, int tempposX, int tempposY, int nowDirection, int futureDirection,Color color,String imagePath) {
        super(x, y, tempposX, tempposY, nowDirection, futureDirection);
        this.x = x;
        this.y = y;
        this.tempposX =tempposX;
        this.tempposY = tempposY;
        this.nowDirection= nowDirection;
        this.futureDirection=futureDirection;
        this.color=color;
        this.imagePath= imagePath;
    }

    public  void wayPoint(){
        do {
            do {
                do {
                    int t = (int) (Math.random()*4);
                    switch (t){
                        case 0:
                            futureDirection = N;
                            break;
                        case 1:
                            futureDirection = S;
                            break;
                        case 2:
                            futureDirection = W;
                            break;
                        case 3:
                            futureDirection = E;
                            break;
                    }
                }while (nowDirection+futureDirection==0);
                tempposX =x;
                tempposY =y;
                if (futureDirection==1){
                    tempposY-=1;
                }
                if (futureDirection==-1){
                    tempposY+=1;
                }if (futureDirection==2){
                    tempposX+=1;
                }
                if (futureDirection==-2){
                    tempposX-=1;
                }
            }while (tempposX<0||tempposX>=map.length||tempposY<0||tempposY>=map[map.length-1].length);
        }while (map[tempposX][tempposY]!=1);
        y = tempposY;
        x = tempposX;
       // System.out.println("正posX:"+x + "正posY:"+y+"正map[posX][posY]:"+map[x][y]);
        nowDirection =futureDirection;
    }

    public void tick() {
     //  wayPoint();
    }
    public void isAttackFunction(java.awt.event.MouseEvent e,GameStats gameStats,EnemyObject enemyObject){
        int eY= e.getX()/60;
        int eX= e.getY()/60;
        System.out.println("mouseX:"+eX +"mousey:"+eY);
        System.out.println("playerX:"+getX() +"playerX:"+getY());

        if ((getX()-1==eX&&getY()==eY)||
                (getX()+1==eX&&getY()==eY)||
                (getX()==eX&&getY()-1==eY)||
                (getX()==eX&&getY()+1==eY)){
            for (int i=0; i<enemyObject.enemyLinked.size();i++){
                if (eX==enemyObject.enemyLinked.get(i).getX()&&
                        eY==enemyObject.enemyLinked.get(i).getY()){
                    JOptionPane.showMessageDialog(null,"敌人收到攻击");
                    enemyObject.enemyLinked.get(i).setHP(enemyObject.enemyLinked.get(i).getHP()-10);
                }
            }
            setAttack(false);
        }
    }
    public boolean isCanMoveWest(PlayerObject playerObject ,EnemyObject enemyObject){
        boolean iscanmovewest= true;
        for (int i=0;i<playerObject.playerLinked.size();i++){
            if (getY()-1==playerObject.playerLinked.get(i).getY()&&getX()==playerObject.playerLinked.get(i).getX()){
                iscanmovewest= false;
            }
        }
        for (int i=0;i<enemyObject.enemyLinked.size();i++){
            if (getY()-1==enemyObject.enemyLinked.get(i).getY()&&getX()==enemyObject.enemyLinked.get(i).getX()){
                iscanmovewest= false;
            }
        }
        return  iscanmovewest;
    }
    public boolean isCanMoveEast(PlayerObject playerObject ,EnemyObject enemyObject){
        boolean iscanmoveeast= true;
        for (int i=0;i<playerObject.playerLinked.size();i++){
            if (getY()+1==playerObject.playerLinked.get(i).getY()&&getX()==playerObject.playerLinked.get(i).getX()){
                iscanmoveeast= false;
            }
        }
        for (int i=0;i<enemyObject.enemyLinked.size();i++){
            if (getY()+1==enemyObject.enemyLinked.get(i).getY()&&getX()==enemyObject.enemyLinked.get(i).getX()){
                iscanmoveeast= false;
            }
        }
        return  iscanmoveeast;
    }
    public boolean isCanMoveNorth(PlayerObject playerObject ,EnemyObject enemyObject){
        boolean iscanmovenorth= true;
        for (int i=0;i<playerObject.playerLinked.size();i++){
            if (getY()==playerObject.playerLinked.get(i).getY()&&getX()-1==playerObject.playerLinked.get(i).getX()){
                iscanmovenorth=false;
            }
        }
        for (int i=0;i<enemyObject.enemyLinked.size();i++){
            if (getY()==enemyObject.enemyLinked.get(i).getY()&&getX()-1==enemyObject.enemyLinked.get(i).getX()){
                iscanmovenorth=false;
            }
        }
        return  iscanmovenorth;
    }
    public boolean isCanMoveSouth(PlayerObject playerObject ,EnemyObject enemyObject){
        boolean iscanmovesouth= true;
        for (int i=0;i<playerObject.playerLinked.size();i++){
            if (getY()==playerObject.playerLinked.get(i).getY()&&getX()+1==playerObject.playerLinked.get(i).getX()){
                iscanmovesouth= false;
            }
        }
        for (int i=0;i<enemyObject.enemyLinked.size();i++){
            if (getY()==enemyObject.enemyLinked.get(i).getY()&&getX()+1==enemyObject.enemyLinked.get(i).getX()){
                iscanmovesouth= false;
            }
        }
        return  iscanmovesouth;
    }
    public boolean isPlayerCanMove(java.awt.event.MouseEvent e,PlayerObject playerObject,EnemyObject enemyObject){
        boolean isplayercanmove =false;
        for (int i=0;i<enemyObject.enemyLinked.size();i++){
            if (e.getX()/60==enemyObject.enemyLinked.get(i).getY()&&e.getY()/60==enemyObject.enemyLinked.get(i).getX()){
                isplayercanmove= true;
            }
        }
        for (int i=0;i<playerObject.playerLinked.size();i++){
            if (e.getX()/60==playerObject.playerLinked.get(i).getY()&&e.getY()/60==playerObject.playerLinked.get(i).getX()){
                isplayercanmove= true;
            }
        }
        return isplayercanmove;
    }
    public boolean isHavePlayer(PlayerObject playerObject){
        boolean ishaveplayer = false;
        for (int i=0;i<playerObject.playerLinked.size();i++){
            if (getY()+1==playerObject.playerLinked.get(i).getY()&&getX()==playerObject.playerLinked.get(i).getX()||
                    getY()-1==playerObject.playerLinked.get(i).getY()&&getX()==playerObject.playerLinked.get(i).getX()||
                    getY()==playerObject.playerLinked.get(i).getY()&&getX()+1==playerObject.playerLinked.get(i).getX()||
                    getY()==playerObject.playerLinked.get(i).getY()&&getX()-1==playerObject.playerLinked.get(i).getX()
                    ){
                ishaveplayer= true;
            }
        }
        return ishaveplayer;
    }

    public  void enemyMove(java.awt.event.MouseEvent e,PlayerObject playerObject ,EnemyObject enemyObject) throws InterruptedException {
        isHavePlayer(playerObject);
        setSeleted(true);
        Thread thread = new Thread();
        thread.sleep(1000);
        setSeleted(false);
        setMove(true);
        thread.sleep(1500);
        setMove(false);
        if (isHavePlayer(playerObject)==false){
            for (int i= 0;i<playerObject.playerLinked.size();i++){
                if (getX()==playerObject.playerLinked.get(i).getX()){
                    if (getY()>playerObject.playerLinked.get(i).getY()){
                        for (int z =0;z<2;z++){
                            if (isCanMoveWest(playerObject,enemyObject)){
                                setY(getY()-1);
                            }
                            thread.sleep(1000);
                        }
                    }else {
                        for (int z =0;z<2;z++){
                            if (isCanMoveEast(playerObject,enemyObject)){
                                setY(getY()+1);
                            }
                            thread.sleep(1000);
                        }
                    }
                    break;
                }else if (getY()==playerObject.playerLinked.get(i).getY()){
                    if (getX()>playerObject.playerLinked.get(i).getX()){
                        for (int z =0;z<2;z++){
                            if (isCanMoveNorth(playerObject,enemyObject)){
                                setX(getX()-1);
                            }
                            thread.sleep(1000);
                        }
                    }else {
                        for (int z =0;z<2;z++){
                            if (isCanMoveSouth(playerObject,enemyObject)){
                                setX(getX()+1);
                            }
                            thread.sleep(1000);
                        }
                    }
                    break;
                }else if (getX()>playerObject.playerLinked.get(i).getX()){
                    for (int z =0;z<2;z++){
                        if (getY()!=playerObject.playerLinked.get(i).getY()&&isCanMoveNorth(playerObject,enemyObject)){
                            setX(getX()-1);
                        }
                        thread.sleep(1000);
                    }//
                    if (getY()>playerObject.playerLinked.get(i).getY()){
                        for (int z =0;z<2;z++){
                            if (isCanMoveWest(playerObject,enemyObject)){
                                setY(getY()-1);
                            }
                            thread.sleep(1000);
                        }

                    }else if (getY()<playerObject.playerLinked.get(i).getY()){
                        for (int z =0;z<2;z++){
                            if (isCanMoveEast(playerObject,enemyObject)){
                                setY(getY()+1);
                            }
                            thread.sleep(1000);
                        }
                    }
                    break;
                }///
                else if (getX()<playerObject.playerLinked.get(i).getX()){
                    for (int z =0;z<2;z++){
                        if (getY()!=playerObject.playerLinked.get(i).getY()&&isCanMoveSouth(playerObject,enemyObject)){
                            setX(getX()+1);
                        }
                        thread.sleep(1000);
                    }//
                    if (getY()>playerObject.playerLinked.get(i).getY()){
                       for (int z=0;z<2;z++){
                           if (isCanMoveWest(playerObject,enemyObject)){
                               setY(getY()-1);
                           }
                           thread.sleep(1000);
                       }

                    }else if (getY()<playerObject.playerLinked.get(i).getY()){
                        for (int z=0;z<2;z++){
                            if (isCanMoveEast(playerObject,enemyObject)){
                                setY(getY()+1);
                            }
                            thread.sleep(1000);
                        }
                    }
                    break;
                }
            }
        }

        if (isHavePlayer(playerObject)){
            setAttack(true);
            thread.sleep(1000);
            setAttack(false);
            thread.sleep(1000);
            JOptionPane.showMessageDialog(null,"攻击玩家");
        }

    }
    public void playMove(java.awt.event.MouseEvent e,GameStats gameStats,PlayerObject playerObject,EnemyObject enemyObject){

        if (e.getX()/60>=0&&e.getX()/60<Maps.map[0].length&&e.getY()/60>=0&&e.getY()/60<Maps.map.length){


            if (isMove()) {
                if (e.getY() / 60 >= getX() - 2 && e.getY() / 60 <= getX() + 2 && e.getX() / 60 >= getY() - 2 && e.getX() / 60 <= getY() + 2) {

                    if (isPlayerCanMove(e,playerObject,enemyObject)==false){
                        setY(e.getX() / 60);
                        setX(e.getY() / 60);
                        setMove(false);
                    }

                }
            }
            if (isAttack){
                isAttackFunction(e,gameStats,enemyObject);
            }
            if (isSeleted){
                if (e.getX()/60==getY()-1&&e.getY()/60==getX()+1){
                    setMove(true);
                    setSeleted(false);
                }else if (e.getX()/60==getY()+1&&e.getY()/60==getX()+1){
                    gameStats.NOWSTATS= gameStats.ENEMYTURN;
                    setSeleted(false);
                    setMove(false);
                    JOptionPane.showMessageDialog(null,"敌人回合");
                }else if (e.getX()/60==getY()&&e.getY()/60==getX()+1){
                    setAttack(true);
                    setSeleted(false);
                }
            }
            if (e.getX()/60==getY()&&e.getY()/60==getX()){
                if (isSeleted){
                    setSeleted(false);
                }else {
                    setSeleted(true);
                    setAttack(false);
                }
            }else {
                setSeleted(false);
            }
        }
    }
    public void render(Graphics g) {
        Image image1 = Toolkit.getDefaultToolkit().getImage("image/move.png");
        Image image2 = Toolkit.getDefaultToolkit().getImage("image/attack.png");
        Image image3 = Toolkit.getDefaultToolkit().getImage("image/over.png");
        if (isAttack()){
            g.setColor(new Color(237, 67, 90,100));
            g.fillRect((getY()-1)*60,(getX())*60,60,60);
            g.fillRect((getY()+1)*60,(getX())*60,60,60);
            g.fillRect((getY())*60,(getX()-1)*60,60,60);
            g.fillRect((getY())*60,(getX()+1)*60,60,60);


        }
        if (isSeleted){
            g.setColor(new Color(33, 18, 143,100));
            g.fillRect((getY()-1)*60,(getX()+1)*60,60,60);
            g.drawImage(image1,(getY()-1)*60,(getX()+1)*60,60,60,null);
            g.fillRect(getY()*60,(getX()+1)*60,60,60);
            g.drawImage(image2,(getY())*60,(getX()+1)*60,60,60,null);
            g.fillRect((getY()+1)*60,(getX()+1)*60,60,60);
            g.drawImage(image3,(getY()+1)*60,(getX()+1)*60,60,60,null);
        }
        if (isMove){
            g.setColor(new Color(255, 33, 60,50));
            for (int i = getX()-2;i<=getX()+2;i++)
                for (int j =getY()-2;j<=getY()+2;j++){
                   if (i>=0&&i<Maps.map.length&&j>=0&&j<Maps.map[0].length){
                    g.fillRect(j*60,i*60,60,60);
                   }
                }
        }
        if (isShowHP()){
            g.setColor(Color.BLUE);
            g.fillRect(3*60,1*60,60,60);
            Image image = Toolkit.getDefaultToolkit().getImage(imagePath);
            g.drawImage(image,3*60,1*60,60,60,null);
            g.fill3DRect(4*60,1*60,6*100,60,true);
            g.setColor(Color.red);
            g.fill3DRect(4*60,1*60,6*getHP(),60,true);


        }
        g.setColor(color);
        g.fillRect(y*60,x*60,60,60);
        Image image = Toolkit.getDefaultToolkit().getImage(imagePath);
        g.drawImage(image,y*60,x*60,60,60,null);
    }
}
